import Qt 4.7
import Qt.labs.components.native 1.0

import "../DesktopComponents"

Item {

    Image {

        id: scoreboardBackground
        source: "pics/scoreboard.jpg"
        anchors.bottom: parent.bottom
        width: parent.width
        height: parent.height
        smooth: true

        property variant colors: ["#FFB90F", "#CaCaCa", "#A67D3D"]

        ListView {

            id: scoreboardToplist
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.topMargin: 20
            anchors.leftMargin: 20

            width: 180; height: 210
            model: players

            delegate: Row {
                spacing: 10

                Image {
                    source: modelData.position == 1 ? "medals/gold.png" : modelData.position == 2 ? "medals/silver.png" : "medals/bronze.png"
                    visible: modelData.position<=3
                    smooth: true
                    width: 50
                    height: 50
                }

                Rectangle {
                    color: "transparent"
                    visible: modelData.position>3
                    width: 50
                    height: 50
                }

                Text { font.bold: true; anchors.verticalCenter: parent.verticalCenter; color: modelData.position <=3 ? scoreboardBackground.colors[modelData.position-1] : "#aaaaaa"; font.pixelSize: 24; text: "#" + modelData.position }
                Text { font.bold: true; anchors.verticalCenter: parent.verticalCenter; color: modelData.position <=3 ? scoreboardBackground.colors[modelData.position-1] : "#aaaaaa"; font.pixelSize: 24; text: modelData.name }
                Text { anchors.verticalCenter: parent.verticalCenter; color: modelData.position <=3 ? scoreboardBackground.colors[modelData.position-1] : "#aaaaaa"; font.pixelSize: 24; text: modelData.score }
            }
        }

        Text {
            id: scoreboardToplistStatus
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.topMargin: 20
            anchors.leftMargin: 20
            text: qsTr("__LOADING__") + emptyString;
            color: "white"
            font.pixelSize: 24;
        }

        Button {

            id: retryLoadScoreboardButton
            text: qsTr("__RETRY__") + emptyString
            anchors.top: scoreboardToplistStatus.bottom
            anchors.left: scoreboardToplistStatus.left
            anchors.topMargin: 20
            visible: false
            onClicked: {
                retryLoadBoard();
            }

        }

        Item {
            id: submitScoreboardSection
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.topMargin: 20
            anchors.rightMargin: 20
            Column {

                anchors.right: parent.right
                spacing: 5
                Text {
                    visible: isPlaying || hasWon
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__LASTSCORE__") + emptyString + ': ' + currentScore; //'Current score: '
                    font.pixelSize: 24;
                }
                Text {
                    visible: !isPlaying && !hasWon
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__LASTSCORE__") + emptyString + ': ' + lastScore; //'Last score: '
                    font.pixelSize: 24;
                }
                Text {
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__BESTSCORE__") + emptyString + ': ' + (maxScore > currentScore ? maxScore : currentScore); //'Best score: '
                }
                Text {
                    visible: streak > 0
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__WINNINGSTREAK__") + emptyString + ": " +streak; //'Winning streak: ' + streak;
                }
                Text {
                    visible: isPlaying || hasWon
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__FINISHFIRST__") + emptyString; //'Finish the current game first!'
                }
                Text {
                    visible: (!isPlaying && !hasWon) && (maxScore <= maxScoreSubmitted)
                    anchors.right: parent.right
                    color: "white"
                    text: qsTr("__WASSUBMITTED__") + emptyString; //'Your best score was submitted!'
                }
                Rectangle {
                    visible: maxScore > maxScoreSubmitted && !isPlaying && !hasWon
                    color: "white"
                    border.color: "black"
                    border.width: 3
                    width: parent.width
                    height:31
                    anchors.right: parent.right
                    TextInput {
                        id: playerNameInput
                        anchors.fill: parent
                        anchors.margins: 5
                        font.pixelSize: 18
                        maximumLength: 16
                        anchors.verticalCenter: parent.verticalCenter
                        text: playerName
                        onAccepted: {
                            closeSoftwareInputPanel();
                            focus = false;
                        }
                    }
                }

                Button {
                    id: submitScoreButton
                    anchors.right: parent.right
                    visible: maxScore > maxScoreSubmitted && !isPlaying && !hasWon
                    text: qsTr("__SUBMIT__") + emptyString
                    onClicked: {
                        playerNameInput.closeSoftwareInputPanel();
                        playerNameInput.focus = false;
                        submitScore(playerNameInput.text, maxScore);
                    }
                }

                //TODO: Reset score
                /*
                Button {
                    id: resetScoreButton
                    anchors.right: parent.right
                    text: qsTr("__RESETSCORE__") + emptyString
                    onClicked: {
                        submitScore(playerNameInput.text, maxScore);
                    }
                }
                */

            }
        }

        Button {
            id: onlineScoreboardButton
            text: qsTr("__ONLINE__") + emptyString
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            anchors.bottomMargin: 20
            anchors.leftMargin: 20
            onClicked: {
                Qt.openUrlExternally("http://evidencehunt.blackwicked.com");
            }
        }
    }

    function retryLoadBoard() {
        scoreboardToplist.visible = false;
        scoreboardToplistStatus.visible = true;
        scoreboardToplistStatus.text = qsTr("__LOADING__") + emptyString;
        retryLoadScoreboardButton.visible = false;
        openScoreboard();
    }

    function hasNoError() {
        scoreboardToplist.visible = true;
        scoreboardToplistStatus.visible = false;
        retryLoadScoreboardButton = false;
    }

    function hasError() {
        scoreboardToplist.visible = false;
        scoreboardToplistStatus.visible = true;
        scoreboardToplistStatus.text = qsTr("__CONNECTIONERROR__") + emptyString;
        retryLoadScoreboardButton.visible = true;
    }

}
